#include "sprite.h"

using namespace sprite;
using namespace heading::math;
extern GLint rotation;

DECLARE_EXTERN_GLEXTENSIONS

void readfile( char*& string, const char* filename ){
	
	FILE* file;
	fopen_s(&file,filename,"r");
	int hfile=_fileno(file);
	int length=_filelength(hfile);
	string=new char[length+1];
	fread(string,sizeof(char),length,file);
	string[length]='\0';
	
	fclose(file);

}

void querysuccess( GLint hvs ){

	int cs;

	glGetShaderiv(hvs,GL_COMPILE_STATUS,&cs);

	if( !cs ){
	
		char* log;

		glGetShaderiv(hvs,GL_INFO_LOG_LENGTH,&cs);
		log=new char[cs+1];

		glGetShaderInfoLog(hvs,cs,NULL,log);
		printf("%s\n",log);

		delete []log;

	}

}

void Getvec4(vec4 vector,float& r, float& g, float& b, float& a){

	r=vector.r;
	g=vector.g;
	b=vector.b;
	a=vector.a;

}

void Setvec4(vec4 vector,float r, float g, float b, float a)
{
	vector.r = r;
	vector.g = g;
	vector.b = b;
	vector.a = a;
}

void SetRotationMatrix(float angle){

	matrix<float,2,2> arr;
	arr[0][0]=cos(angle);
	arr[0][1]=-sin(angle);
	arr[1][0]=sin(angle);
	arr[1][1]=cos(angle);
	float rot[4];
	for(int i=0;i<2;i++){

		for(int j=0;j<2;j++){

			rot[2*i+j]=arr[i][j];
		
		}
	
	}

	glUniformMatrix2fv(rotation,1,GL_FALSE,rot);

}

void SamplingPos(float x, float y, float width, float height,float fullwidth, float fullheight, float& s,float& t,float& u,float& v){	

	s=(2*x-fullwidth)/fullwidth;
	t=(2*y-fullheight)/fullheight;
	u=(2*(x+width)-fullwidth)/fullwidth;
	v=(2*(y+height)-fullheight)/fullheight;

}

void SamplingTexcoord(float x, float y, float width, float height,float fullwidth, float fullheight, float& a,float& b,float& c,float& d){	

	a=x/fullwidth;
	b=y/fullheight;
	c=(x+width)/fullwidth;
	d=(y+height)/fullheight;

}